$(function () {
    //如果是专业
    findSubjectList(function (result) {
        var parentList = result[0];
        if(parentList!=null && parentList.length>0){
            //如果超过8个父级专业，则显示出切换专业的按钮
            if(parentList.length>8){
                $("#switchBit").removeClass('slideBtn');
            }
            var context = setSubjectHtml(parentList);
            $("#parent-subject-list-box").html(context);
            $("#parent-subject-list-box ol>li").click(function () {
                $("#parent-subject-list-box ol>li").removeClass('current');
                var _this = $(this);
                _this.addClass('current');
            });
            $("#chooseSubject").click(function () {
                var currentLi = $("#parent-subject-list-box ol>.current");
                if(currentLi==null || currentLi.length ==0){
                    icModale(0,'','请选择一个专业');
                    return;
                }
                var subjectId = currentLi.data('id');
                //隐藏专业
                $("#parent-to-this-box").addClass("hide");
                //显示科目
                $("#parent-to-child-box").removeClass("hide");
                var childList = result[subjectId];
                var childContext = setSubjectHtml(childList);
                $("#child-subject-list-box").html(childContext);
                $("#child-subject-list-box ol>li").click(function () {
                    var _this = $(this);
                    if(_this.hasClass('current')){
                        _this.removeClass('current');
                    }else{
                        _this.addClass('current');
                    }
                });
                $(".upBackTo").click(function(){
                    //显示科目
                    $("#parent-to-child-box").addClass("hide");
                    //隐藏专业
                    $("#parent-to-this-box").removeClass("hide");
                });
                $("#studyCenter").click(function () {
                    var currentLi = $("#child-subject-list-box ol>.current");
                    if(currentLi==null || currentLi.length==0){
                        icModale(0,'','请选择一个或多个科目');
                        return;
                    }
                    var childIds = currentLi.map(function (key,value) {
                        return $(value).data('id');
                    }).get().join(",");
                    $.ajax({
                        url:baseloaction +'/web/user/setUserSubjectIds',
                        type:'post',
                        dataType:'json',
                        data:{
                            'sessionId':key,
                            'parentId':subjectId,
                            'childIds':childIds,
                            'autoThirty':autoThirty
                        },
                        success:function (result) {
                            if(result.success==true){
                                var _url = result.url;
                                if(_url==null || $.trim(_url)==''){
                                    window.location.href = baseloaction +'/web/uc/course/user/index';
                                }else{
                                    window.location.href =baseloaction + _url;
                                }
                            }else{
                                icModale(0,'',result.message);
                            }
                        },
                        error:function (error) {
                            icModale(0,'','完善专业数据错误，进入个人中心失败');
                        }
                    });
                });
            });
        }
    });
    function setSubjectHtml(subjectList) {
        if(subjectList!=null && subjectList.length>0){
            var context = '<div class="item active"><ol>';
            $.each(subjectList,function (index,subjct) {
                if(index % 8 ==0){
                    if(index==0 && subjectList.length==1){
                        context +='</ol></div>';
                    }else{
                        if(index > 0){
                            context +='</ol></div><div class="item"><ol>';
                        }
                    }
                }
                context+='<li data-id="'+subjct.id+'"><a href="javascript:void(0);" class="regTxt" >'+subjct.subjectName+'</a></li>';
                if(index==(subjectList.length-1)){
                    context+='</ol></div>';
                }
            });
            return context;
        }
        return '';
    }
});